﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.WebUI.CentralEmail
{
    public partial class ContaEmail : Page
    {
        private static List<Domain.ContaEmail> ContaEmails { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            ContaEmails = new Domain.ContaEmail().SelecionarAtivos();

            PreencherListView();
        }

        private void PreencherListView()
        {
            lsvContaEmail.DataSource = ContaEmails;
            lsvContaEmail.DataBind();
        }

        protected void lsvContaEmail_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            var item = (ListViewDataItem) e.Item;
            var contalEmail = (Domain.ContaEmail) item.DataItem;

            ((Label)item.FindControl("lblDisplayName")).Text = contalEmail.DisplayName;
            ((Label)item.FindControl("lblEmail")).Text = contalEmail.Email;
            ((Label)item.FindControl("lblSmtpHost")).Text = contalEmail.SmtpHost;
            ((Label)item.FindControl("lblSmtpPort")).Text = contalEmail.SmtpPort.ToString();

            ((ImageButton)item.FindControl("imbEditar")).Attributes.Add("Id", contalEmail.Id.ToString());
            ((ImageButton)item.FindControl("imbExcluir")).Attributes.Add("Id", contalEmail.Id.ToString());
        }

        protected void imbExcluir_Click(object sender, ImageClickEventArgs e)
        {
            int id = int.Parse(((ImageButton)sender).Attributes["Id"]);

            var conta = (from contaEmail in ContaEmails
                         where contaEmail.Id.Equals(id)
                         select contaEmail).First();

            ContaEmails.Remove(conta);
            conta.Inativar();
        }

        protected void lnkAdicionar_Click(object sender, EventArgs e)
        {
            pnlCampos.Visible = true;
            lnkAdicionar.Visible = false;
        }

        protected void lnkSalvar_Click(object sender, EventArgs e)
        {
            var conta = new Domain.ContaEmail
                            {
                                Ativo = true,
                                DataAlteracao = DateTime.Now,
                                DataInclusao = DateTime.Now,
                                DisplayName = txtDisplayName.Text,
                                Email = Email.TxtEmail.Text,
                                Senha = Domain.Helpers.Util.Criptografar(Senha.TxtSenha.Text),
                                SmtpHost = txtSmtpHost.Text,
                                SmtpPort = int.Parse(txtSmtpPort.Text)
                            };

            conta.Save();
            ContaEmails.Add(conta);
            PreencherListView();
            pnlCampos.Visible = false;
            lnkAdicionar.Visible = true;
        }
    }
}